<?php
/**
 * 北斗相关
 * 发仔<i@fazi.me> 2018-07-24
 */
namespace app\open\controller;

use app\open\model;
/**
 * @name【1.北斗相关】
 */
use app\base\service\Redis;
use app\open\validate;
class Beidou extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        //redis定义
        $redisServer    = Redis::getInstance();
        $this->redis    = $redisServer->redis;
    }


    public function getRedisData()
    {
        $prefix = '*beidou*';
        $ret = $this->redis->keys($prefix);
        if (empty($ret)) {
            return $this->retError(123);
        }
        $brr = [];
        $nowTime = time();
        foreach ($ret as $v) {
            $arr = $this->redis->get($v);
            $arr = json_decode($arr, true);
            $arr['time'] - $nowTime > 15 * 60 && $arr['alarm'] = '终端没电';
            $arr['time'] - $nowTime > 15 * 60 && $arr['state'] = ' 停止 离线 不定位';
            $brr[] = $arr;
        }
        return $brr;
    }

    /**
     * @name【1.1 所有车辆北斗信息】
     * @desc【】
     * @method【post】
     * @param【
     * fields $ int $ car_id,number,entrust_company_id,company_id $ 车辆字段 #
     * company_id $ int $  $ 公司id #
     * car_number $ string $ 闽C001 $ 车牌
     * 】
     * @table【car】
     * @return【】
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCar()
    {
        //调用集成
        $param = $this->getParam(new validate\BeidouValidate(),'all');
        $cars = $this->getRedisData();
        if (!$cars) {
            return $this->retError('数据请求异常，请重新发起请求。');
        }
        $data = $where = [];
        //车牌号
        $numbers = array_column($cars, null, 'carno');
        $where['is_beidou'] = 1;
        $param['car_number'] && $where['number'] = ['LIKE' ,"%{$param['car_number']}%"];
        //公司
        if(isset($param['company_id'])){
            $where['entrust_company_id|company_id'] = $param['company_id'];
            unset($param['company_id']);
        }
        //所有车辆
        $fileds = $param['fields'] ?: 'car_id,number,entrust_company_id,company_id';
        $model = new model\Car();
        $list = $model->field($fileds)->where($where)->select();
        foreach ($list AS $id => $value) {
            $beidou = $numbers[$value['number']];
            $value['loc'] = [$beidou['lon'], $beidou['lat']];
            $states = explode(' ', trim($beidou['state']));
            switch ($states[0]) {
                case '停止':
                    $value['state'] = 1;
                    break;
                case '行驶':
                    $value['state'] = 2;
                    break;
                case '':
                    $value['state'] = 4;
                    break;
            }
            if ($states[1] == '离线') $value['state'] = 3;
            $value['alarm'] = $beidou['alarm'];
            $value['time'] = date('Y-m-d H:i', $beidou['time']);
            $data[$id] = $value;
        }
        return $this->res($data);

    }
    /**
     * @name【1.2 北斗车辆获取所有订单】
     * @desc【】
     * @method【post】
     * @param【
     * fields $ int $ car_id,number,entrust_company_id,company_id $ 车辆字段 #
     * company_id $ int $  $ 公司id #
     * car_number $ string $ 闽C001 $ 车牌
     * 】
     * @table【car】
     * @return【】
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOrder() {

    }

}